package cn.codeforfun.gitee.api.repo;

import cn.codeforfun.gitee.api.GiteeService;
import cn.codeforfun.gitee.bean.GiteeBranch;
import cn.codeforfun.gitee.bean.GiteeToken;
import cn.codeforfun.gitee.bean.repo.request.*;
import cn.codeforfun.gitee.bean.repo.response.GiteeRepoResponse;
import cn.codeforfun.gitee.utils.JsonUtils;
import cn.codeforfun.gitee.utils.OkHttpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

import static cn.codeforfun.gitee.constant.GiteeConstant.URL_PREFIX_API;

/**
 * @author wangbin
 */
@RequiredArgsConstructor
@Slf4j
public class GiteeRepoServiceImpl implements GiteeRepoService {
    private final GiteeService giteeService;

    @Override
    public List<GiteeBranch> listBranches(GiteeListBranchesRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("listBranches request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/repos/" + request.getOwner() + "/" + request.getRepo() + "/branches";
        String result = OkHttpUtils.get(url, request);
        log.debug("listBranches result:{}", result);
        return JsonUtils.toBeanList(result, GiteeBranch.class);
    }

    @Override
    public GiteeBranch createBranch(GiteeCreateBranchRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("createBranch request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/repos/" + request.getOwner() + "/" + request.getRepo() + "/branches";
        String result = OkHttpUtils.postForm(url, request);
        log.debug("createBranch result:{}", result);
        return JsonUtils.toBean(result, GiteeBranch.class);
    }

    @Override
    public GiteeBranch protectionBranch(GiteeProtectionBranchRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("protectionBranch request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/repos/" + request.getOwner() + "/" + request.getRepo() + "/branches/" + request.getBranch() + "/protection";
        String result = OkHttpUtils.putForm(url, request);
        log.debug("protectionBranch result:{}", result);
        return JsonUtils.toBean(result, GiteeBranch.class);
    }

    @Override
    public List<GiteeRepoResponse> listRepos(GiteeRepoQueryRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("findUserRepos request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/user/repos";
        String result = OkHttpUtils.get(url, request);
        log.debug("findUserRepos result:{}", result);
        return JsonUtils.toBeanList(result, GiteeRepoResponse.class);
    }

    @Override
    public GiteeRepoResponse createRepo(GiteeRepoRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("createUserRepo request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/user/repos";
        String result = OkHttpUtils.postForm(url, request);
        log.debug("createUserRepo result:{}", result);
        return JsonUtils.toBean(result, GiteeRepoResponse.class);
    }

    @Override
    public void deleteRepo(GiteeDeleteRepoRequest request) throws IOException {
        GiteeToken token = giteeService.getToken();
        request.setAccessToken(token.getAccessToken());
        giteeService.validate(request);
        log.debug("deleteRepo request:{}", JsonUtils.toJson(request));
        String url = URL_PREFIX_API + "/repos/" + request.getOwner() + "/" + request.getRepo();
        String result = OkHttpUtils.delete(url, request);
        log.debug("deleteRepo result:{}", result);
    }

}
